<template>
  <div class="modifyManager common-container">
    <div class="common-top">
      <span class="left">
        <span class="go-back" @click.stop="$router.go(-1)">返回</span>
      </span>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
    <el-form ref="form" :model="form">
      <el-card class="box-card" style="margin-top: 10px">
        <div slot="header" class="clearfix">
          <span>全局配置名称</span>
        </div>
        <el-input v-model="form.name"></el-input>
      </el-card>
      <el-card style="margin-top: 10px" class="box-card" v-for="(item, index) in fieldsList" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ item.fieldName }}</span>
        </div>
        <el-form-item
          v-for="(fItem, fIndex) in item.fieldsList"
          :key="fIndex"
          required
          :label="fItem.detail">
          <el-input v-if="fItem.onOff" v-model="fItem.def.value">
            <template slot="prepend">
              是否启用：<el-switch v-model="fItem.def.enable"></el-switch>
            </template>
          </el-input>
          <template v-else>
            <el-switch v-if="fItem.type === 'boolean'" v-model="fItem.def"></el-switch>
            <el-input v-else-if="fItem.type === 'int'" v-model="fItem.def" type="number"></el-input>
            <el-input v-else v-model="fItem.def"></el-input>
          </template>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>
<script>
import HhxApi from "@/api/";
export default {
  name: "ModifyManager",
  data() {
    return {
      form: {
        enable: true,
        formId: 0,
        name: "",
        setting: {
          freeEmployeeCount: 0,
          freeStoreCount: 0,
          shareRewardDay: {
            enable: true,
            value: 0,
          },
        },
      },
      genderOptions: [
        {
          label: "男",
          value: "male",
        },
        {
          label: "女",
          value: "female",
        },
      ],
    };
  },
  methods: {
    getInfo() {
      HhxApi.global
        .detail({
          targetId: this.$route.query.id,
        })
        .then((res) => {
          this.form = {
            enable: res.data.enable,
            formId: res.data.id,
            name: res.data.name,
            setting: {
              freeEmployeeCount: res.data.setting.freeEmployeeCount,
              freeStoreCount: res.data.setting.freeStoreCount,
              shareRewardDay: res.data.setting.shareRewardDay,
            },
          };
        });
    },
    checkInfo() {
      if (String(this.form.name).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写名称",
        });
        return false;
      }
      if (String(this.form.mobile).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写账号",
        });
        return false;
      }
      if (String(this.form.password).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写密码",
        });
        return false;
      }
      return true;
    },
    save() {
      if (!this.checkInfo()) {
        return;
      }
      HhxApi.global.edit(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.$router.go(-1);
        }
      });
    },
  },
  mounted() {
    if (this.$route.query.type == "edit") {
      this.getInfo();
    }
  },
};
</script>
<style scoped lang="scss">
.modifyManager {
  .modify-bot {
    width: 70%;
    padding: 30px 10px;
  }
}
</style>
